#!/usr/bin/env bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

umask 002

. $CCI_SRC/container/defconfig.sh

load_service_config

cat > $CCI_SRC/container/dnsmasq/dnsmasq.d/${lab}.conf <<EOF
interface=$interface
dhcp-authoritative
dhcp-range=$dhcp_range
EOF

SCHED_HOST=$(awk '/^SCHED_HOST:\s/ {print $2;exit}' /etc/compass-ci/defaults/*.yaml)
sed -i "s%172.17.0.1%${SCHED_HOST}%g" /tftpboot/boot.ipxe

get_smtp_conf()
{
	SMTP_DOMAIN=${ROBOT_EMAIL_ADDRESS##*@}
	line=$(grep -m1 -e "smtp.$SMTP_DOMAIN" -e "@$SMTP_DOMAIN" $CCI_SRC/sparrow/well-known-smtp-servers)
	if [ -n "$line" ]; then
		SMTP_PORT=$(echo $line | awk '{print $1}')
		SMTP_ADDRESS=$(echo $line | awk '{print $2}')
	else
		SMTP_PORT="25"
		SMTP_ADDRESS="smtp.${SMTP_DOMAIN}"
	fi
}

[ -n "$SMTP_ADDRESS" ] && [ -n "$SMTP_PORT" ] && [ -n "$SMTP_DOMAIN" ] || get_smtp_conf

cat > /etc/compass-ci/accounts/robot-email.yaml <<EOF
SMTP_ADDRESS: $SMTP_ADDRESS
SMTP_PORT: $SMTP_PORT
SMTP_DOMAIN: $SMTP_DOMAIN
ROBOT_EMAIL_ADDRESS: $ROBOT_EMAIL_ADDRESS
ROBOT_EMAIL_PASSWORD: $ROBOT_EMAIL_PASSWORD
EOF
chmod 644 /etc/compass-ci/accounts/robot-email.yaml
